package com.woniu01;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sid", 1);
        map.put("sname", "zs");
        map.put("age", 11);
        Class<?> clazz = Class.forName("com.woniu01.Student");
        mapTobean(map, clazz);
        System.out.println(mapTobean(map, clazz));
    }
    public static <T> T mapTobean(Map<String, Object> map,Class<T> clazz) throws InstantiationException, IllegalAccessException {
        T obj = (T) clazz.newInstance();
        for (String key : map.keySet()) {
            try {
                Field field = clazz.getDeclaredField(key);
                field.setAccessible(true);
                field.set(obj, map.get(key));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return obj;
    }


}
